package cn.itsource.ldj.org.service.impl;

import cn.itsource.ldj.base.service.impl.BaseServiceImpl;
import cn.itsource.ldj.org.domain.EmpLoyee;
import cn.itsource.ldj.org.domain.Shoplog;
import cn.itsource.ldj.org.domain.Shop;
import cn.itsource.ldj.org.mapper.EmpLoyeeMapper;
import cn.itsource.ldj.org.mapper.ShopLogMapper;
import cn.itsource.ldj.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService  {

    @Autowired
    private EmpLoyeeMapper empLoyeeMapper;
    @Autowired
    private ShopLogMapper shopLogMapper;

    /* 邮箱核心对象 */
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 设置商家管理员的默认状态
        shop.getAdmin().setState(0);
        // 添加管理商家的管理员
        empLoyeeMapper.save(shop.getAdmin());
        // 设置商家入驻的默认状态
        shop.setState(0);

        // 添加商家
        super.save(shop);
    }

    /* 驳回 */
    @Override
    @Transactional
    public void noexamine(Shoplog shoplog) throws MessagingException {

        // 修改店铺状态为驳回
        // 根据id查询审核的店铺
        Shop oldShop = super.findOne(shoplog.getShop_id());
        // 将状态修改为驳回
        oldShop.setState(-1);
        // 重新修改到数据库保存
        super.update(oldShop);

        // 设置审查店铺的审查人
        EmpLoyee empLoyee = new EmpLoyee();
        empLoyee.setId(1L);
        shoplog.setEmpLoyee(empLoyee);
        // 添加日志文件
        shopLogMapper.save(shoplog);



        /* 发送邮件 */
        // 查询获取店鋪管理員对象
        System.out.println("店铺管理员id" + shoplog.getAdmin_id());
        EmpLoyee empLoyee1 = empLoyeeMapper.findOne(shoplog.getAdmin_id());
        System.out.println("店铺管理员" + empLoyee1);
        // 获取店铺管理员email
        String email = empLoyee1.getEmail();

        // 创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 发送复杂邮件工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        // 设置发件人
        helper.setFrom("xuan2236079100@163.com");
        // 设置收件人
        helper.setTo(email);
        // 设置邮件名称
        helper.setSubject("审核未通过");
        System.out.println(shoplog.getShop_id());
        // 设置邮件内容
        helper.setText("<h1>"+ shoplog.getContent() +"</h1></br>"+
                "<a href='http://127.0.0.1:8081/#/register2?id="+ shoplog.getShop_id() +"'>点击修改</a>",true);

        // 发送邮件
        javaMailSender.send(mimeMessage);

    }

    /* 审核通过 */
    @Override
    public void pass(Shoplog shoplog) throws Exception {

        // 修改店铺状态为驳回
        // 根据id查询审核的店铺
        Shop oldShop = super.findOne(shoplog.getShop_id());
        // 将状态修改为驳回
        oldShop.setState(1);
        // 重新修改到数据库保存
        super.update(oldShop);


        /* 发送邮件 */
        // 查询获取店鋪管理員对象
        System.out.println("店铺管理员id" + shoplog.getAdmin_id());
        EmpLoyee empLoyee1 = empLoyeeMapper.findOne(shoplog.getAdmin_id());
        System.out.println("店铺管理员" + empLoyee1);
        // 获取店铺管理员email
        String email = empLoyee1.getEmail();

        // 创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 发送复杂邮件工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        // 设置发件人
        helper.setFrom("xuan2236079100@163.com");
        // 设置收件人
        helper.setTo(email);
        // 设置邮件名称
        helper.setSubject("审核通过");
        System.out.println(shoplog.getShop_id());
        // 设置邮件内容
        helper.setText("<h1>请激活</h1></br>"+
                "<a href='http://127.0.0.1:8080/shop/activate?id="+ empLoyee1.getId() +"'>点击激活</a>",true);

        // 发送邮件
        javaMailSender.send(mimeMessage);
    }

    /* 激活 */
    @Override
    public void activate(Long id) {
        // 查询店铺管理员账号
        EmpLoyee empLoyee = empLoyeeMapper.findOne(id);
        // 修改状态为激活成功
        empLoyee.setState(1);
        // 修改到数据库
        empLoyeeMapper.update(empLoyee);
    }
}
